package com.xctech.yace.main;

import com.xctech.yace.dto.MetaDataDTO;
import com.xctech.yace.pool.ProduceThreadPool;
import com.xctech.yace.service.KafkaSend;
import com.xctech.yace.service.TradeO32Service;
import com.xctech.yace.util.DirUtil;
import com.xctech.yace.util.TimeUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;


public class YaceSendFromDB {
    public static void main(String[] args) {
        //2.串行发送
//        syncSend("0x0004.004.00003b8b","YACE");
    }

    public static List<Map<String,Object>> selectMap(String sql){
        String filePath = DirUtil.getOrgPath()+"conf/db.properties";
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Map<String,Object>> metas = TradeO32Service.selectSql(sql, properties);
        return metas;
    }


    public static Boolean syncSend(String xid,String topic) {
        Boolean result = false;

        KafkaSend send = new KafkaSend();

        String filePath = DirUtil.getOrgPath()+"conf/db.properties";
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //查询数据库，相同的xid，offset从小到大
        List<MetaDataDTO> metas = TradeO32Service.select(xid, properties);
        for(MetaDataDTO dto:metas){
            String msg = dto.getMetaJson();
            result = send.sendMsgSync(topic, msg);
        }
        return result;
        //设定线程池几秒后关闭
//        ProduceThreadPool.stopPool(1000l*5);
    }

    public static Boolean sendNMsg(String topic,String[] msgs){
        Boolean result = false;
        KafkaSend send = new KafkaSend();
        for(int i=0;i < msgs.length;i++){
            result = send.sendMsgSync(topic, msgs[i].trim());
        }
        return result;
    }

}
